home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
HAM Radio 1997
/
HAM Radio 1997.iso
/
vcls
/
compress
/
compmain.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
1996-04-08
|
23KB
|
660 lines
(*
CompDemo V1.5 for TCompress Components V1.5
You are free to amend, adjust, improve, update, borrow, alter and muck about
with this demonstration program at will.
However, if you redistribute the amended source together with the TCompress
components, please be sure to include ALL the files that came with it
(incl. Compress.hlp, Readme.txt and the ORIGINAL COMPDEMO source). Thanks.
Hint: To find the code which makes use of the TCompress components, search
for Compress1, CDBImage1 and CDBMemo1 references...
Enjoy.
*)
{$D-} { Don't need debugging info, thanks... }
unit Compmain;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, Compress, StdCtrls, DB, DBTables, DBCtrls,
CompCtrl, ExtCtrls, Buttons, FileCtrl, Mask;
type
TForm1 = class(TForm)
Table1: TTable;
DBNavigator1: TDBNavigator;
DataSource1: TDataSource;
Compress1: TCompress;
Table1SpeciesNo: TFloatField;
Table1Category: TStringField;
Table1Common_Name: TStringField;
Table1SpeciesName: TStringField;
Table1Lengthcm: TFloatField;
Table1Length_In: TFloatField;
CDBImage1: TCDBImage;
CDBMemo1: TCDBMemo;
CMethod: TRadioGroup;
Memo2: TMemo;
Shape1: TShape;
GroupBox1: TGroupBox;
FL: TFileListBox;
DL: TDirectoryListBox;
DCB: TDriveComboBox;
ArchiveGroup: TGroupBox;
ArchiveLabel: TLabel;
archivefile: TEdit;
Label2: TLabel;
ListBox1: TListBox;
Fishname: TDBEdit;
Memo4: TMemo;
Memo3: TMemo;
Memo5: TMemo;
Memo6: TMemo;
DBText1: TDBText;
Memo1: TMemo;
Button1: TButton;
Panel1: TPanel;
Bevel1: TBevel;
Time: TLabel;
Percentage: TLabel;
TimeLabel: TLabel;
Label7: TLabel;
Trashcan: TImage;
Image1: TImage;
Button2: TButton;
procedure CompressOneFile(var fname: String);
procedure ResetFileInfo;
function GetDir: string;
function GetDummyFilename(generatefrom: string; ext: string): string;
procedure handleDropField(Source: TObject; archivetoo: Boolean);
procedure CompressFiles;
procedure CheckArchiveFile;
function getCompressionMethod: TCompressionMethod;
procedure showInfo;
procedure FormCreate(Sender: TObject);
procedure showfiles;
procedure ExpandDelete(Operation: TCProcessMode; All: Boolean);
procedure archivefileChange(Sender: TObject);
procedure CMethodClick(Sender: TObject);
procedure DLDragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure CDBMemo1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure CDBImage1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure archivefileDragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure archivefileDragDrop(Sender, Source: TObject; X, Y: Integer);
procedure DLDragDrop(Sender, Source: TObject; X, Y: Integer);
procedure TrashcanDragDrop(Sender, Source: TObject; X, Y: Integer);
procedure FormDestroy(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
procedure CDBImage1DragDrop(Sender, Source: TObject; X, Y: Integer);
procedure CDBMemo1DragDrop(Sender, Source: TObject; X, Y: Integer);
procedure CDBImage1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure CDBMemo1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Table1AfterPost(DataSet: TDataset);
procedure Button1Click(Sender: TObject);
procedure FLClick(Sender: TObject);
procedure Compress1CheckFile(var filepath: OpenString;
mode: TCProcessMode);
procedure Panel1Click(Sender: TObject);
procedure FormClick(Sender: TObject);
procedure GroupBox1Click(Sender: TObject);
procedure TrashcanDragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
var FileList: TStringList; { holds information about our archive files }
saveCompressionMethod: Integer; { see ListBox1.click }
const ShowFileInfoColor :Tcolor = clGray; { see Listbox1.click }
{ Example of accessing the TCompress performance properties }
procedure Tform1.showinfo;
begin
ResetFileInfo;
Time.caption:=Format('%-5.1fsecs',[Compress1.CompressionTime/1000.0]{[f]});
Percentage.caption:=IntToStr(Compress1.CompressedPercentage)+'%';
end;
{ Example of getting a list of files in a multi-file archive }
procedure TForm1.showfiles;
begin
listbox1.clear;
FileList.clear;
if not FileExists(archivefile.Text) then exit;
Compress1.ScanCompressedFile(ArchiveFile.Text,Filelist);
ListBox1.Items.addStrings(FileList); { and File info objects are
there too -- see ListBox1Click and FormDestroy }
end;
{ Example of OnCheckFile user interface handling routine }
{ Example of expanding/deleting one or more files from a multi-file archive }
procedure TForm1.ExpandDelete(Operation: TCProcessMode; All: Boolean);
var s: Tstringlist;
count: Integer;
begin
if (All and (Listbox1.Items.count > 0)) or (Listbox1.selcount>0) then { something is... }
begin
s:=Tstringlist.create;
try
if All then
s.addStrings(ListBox1.Items)
else
for count :=0 to Listbox1.ITems.count-1 do
if Listbox1.selected[count] then
s.add(Listbox1.items[count]);
if Operation=cmExpand then { expand }
compress1.expandfiles(ArchiveFile.Text,s)
else
compress1.deletefiles(ArchiveFile.Text,s);
showinfo;
showfiles; { also clears selections... }
finally
s.free;
Screen.Cursor := crDefault;
end;
end;
end;
{ Example of compressing a SINGLE file into an archive }
procedure TForm1.CompressOneFile(var fname: String);
begin
Compress1.CompressFile(ArchiveFile.Text,fname,getCompressionMethod);
showInfo;
showfiles;
Screen.Cursor := crDefault;
DeleteFile(fname); { because for this example we're creating TEMP files only... }
end;
{ Example of compressing MULTIPLE files into an archive }
procedure TForm1.CompressFiles;
var s: Tstringlist;
Count: Integer;
begin
if FL.selcount>0 then { something is... }
begin
s:=TStringlist.Create;
try
for count :=0 to FL.Items.count-1 do
if FL.selected[count] then
s.add(FL.items[count]);
Compress1.CompressFiles(ArchiveFile.Text,s,getCompressionMethod);
showInfo;
showfiles;
finally;
s.free;
Screen.Cursor := crDefault;
end;
end;
end;
{ Examples of setting/loading/shifting image blobs }
procedure TForm1.CDBImage1DragDrop(Sender, Source: TObject; X, Y: Integer);
var filepath: String;
mem: TMemoryStream; { for loading image from an archived file }
begin
if Source=Sender then exit; { nowt to do }
if (Sender is TCDBImage) and (not Table1.active) then
begin
showmessage('Can''t do this unless table has been opened...');
exit;
end;
Screen.Cursor:= crHourGlass;
if (Source = Image1) and (Sender is TCDBImage) then
CDBImage1.picture.bitmap.Assign(Image1.Picture.bitmap)
else if (Source is TCDBImage) and (Sender = Image1) then
Image1.picture.bitmap.Assign(CDBImage1.Picture.Bitmap)
else
begin { Have we got an image? }
filepath := '';
if (Source is TListBox) and (Listbox1.selcount = 1) then
filepath:=ListBox1.Items[Listbox1.ItemIndex] { archive list }
else if (Source is TFileListBox) and (FL.selcount=1) then
filepath:=FL.Items[FL.ItemIndex]; { file list }
if ExtractFileExt(filepath)<>'.bmp' then
begin
MessageBeep(1);
showmessage('Must be a .BMP file...')
end else begin { ok, here we go... }
if Source is TListBox then { must first extract file... }
begin { Note: Registered users will get the source of two FASTER ways
of going about this (no expanded file needed) }
Compress1.ExpandFile(filepath,ArchiveFile.Text);
Screen.cursor := crDefault; { as our OnCheckFile sets it on }
if filepath='' then exit; { was skipped on confirmation }
end;
Screen.Cursor:= crHourGlass;
if Sender = Image1 then
Image1.Picture.Bitmap.LoadFromfile(filepath)
else
CDBImage1.Picture.Bitmap.LoadFromFile(filepath);
end; { else }
end;
if Table1.active then Table1.post; { save immediately if updated }
if not Image1.Picture.Bitmap.Empty then Memo1.visible := False; { got a piccy showing... }
Screen.Cursor:= crDefault;
end;
{ Examples of setting/loading/shifting CDBMemo blobs }
procedure TForm1.CDBMemo1DragDrop(Sender, Source: TObject; X, Y: Integer);
var filepath: String;
f: Tfilestream;
mem: TMemoryStream; { for loading text from an archived file }
begin
filepath := ''; { in case fails }
if (Source is TListBox) and (Listbox1.selcount = 1) then
filepath:=ListBox1.Items[Listbox1.ItemIndex] { archive list }
else if (Source is TFileListBox) and (FL.selcount=1) then
filepath:=FL.Items[FL.ItemIndex]; { file list }
if ExtractFileExt(filepath)<>'.txt' then
begin
MessageBeep(1);
showmessage('Must be a .TXT file...')
end else begin { ok, here we go... }
if Source is TListBox then { must first extract file... }
begin { Note: Registered users will get the source of two FASTER ways
of going about this (no expanded file needed) }
Compress1.ExpandFile(filepath,ArchiveFile.Text);
Screen.cursor := crDefault; { as our OnCheckFile sets it on }
if filepath='' then exit; { was skipped on confirmation }
end;
Screen.Cursor:= crHourGlass;
CDBMemo1.Lines.LoadfromFile(filepath)
end;
if Table1.active then Table1.post; { save immediately }
Screen.Cursor:= crDefault;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
CheckArchiveFile; { old V1.0 archive deletion... }
fileList := TStringList.create; { keeps track of our archive files for display etc. }
SendMessage(ListBox1.handle,LB_SetHorizontalExtent,300,LongInt(0));
saveCompressionMethod := -1; { see Listbox1.click }
showfiles; { show files in archive (if any)... }
try
DL.Directory := '\DELPHI\IMAGES\BACKGRND';
except on EInOutError do ; { nowt, let it default }
end;
try Table1.Active := True;
DataSource1.Edit;
except
on EDBEngineError do
showmessage('The BLOB compression portion of this demonstration'+#13+
'requires that the DBDEMOS alias be set up and pointing'+#13+
'to the BIOLIFE.DB table in \DELPHI\DEMOS\DATA.'+#13+#13+
'-- as this is not currently the case, the BLOB demonstration'+#13+
'is disabled.');
on EUnrecognizedCompressionMethod do
showmessage('Your BIOLIFE database may have been compressed using the LZW'+#13+
'compression in TCompress v1.0. As LZH replaces LZW in V1.5,'+#13+
'please revert to an uncompressed backup of BIOLIFE.*, or use the'+#13+
'LZW source which comes with registered versions of TCompress v1.5.');
end; {try }
if not Table1.Active then { something went wrong... }
begin
CDBImage1.visible:=False;
CDBMemo1.visible:=False;
DBNavigator1.visible:=False;
Memo1.visible:=False;
Memo2.visible := True;
end;
end;
function TForm1.GetDir: string; { called below and in GetDummyFileName }
begin
Result := DL.Directory;
if Copy(Result,Length(Result),1)<>'\' then { not already \'d? }
Result := Result+'\';
end;
{ V1.5 check: Remove a V1.0 archive file if it is there... }
procedure TForm1.CheckArchiveFile;
var fa: Longint;
begin
fa := FileAge(ArchiveFile.Text);
if (fa<>-1) and (fa < 522890500) then { must be v1.0 -- kill it }
DeleteFile(ArchiveFile.Text);
end;
procedure TForm1.archivefileChange(Sender: TObject);
begin
CheckArchiveFile; { remove old V1.0 archive if we find it is one }
showfiles;
end;
function TForm1.getCompressionMethod: TCompressionMethod;
begin
result := coNone; { default }
case CMethod.ItemIndex of
1: result := coRLE;
2: result := coLZH;
end;
end;
procedure TForm1.CMethodClick(Sender: TObject);
var meth: TCompressionMethod;
begin
CDBIMage1.CompressionMethod := getCompressionMethod;
CDBMemo1.CompressionMethod := getCompressionMethod;
end;
procedure TForm1.DLDragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
accept := True;
if ((Sender is TDirectoryListBox) and (Source is TFileListBox)) or
(Source=Trashcan) then
accept := False; { fair enough? }
end;
procedure TForm1.CDBMemo1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
accept := (Source is TFileListBox) or (Source is TListBox);
end;
procedure TForm1.CDBImage1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
accept := (Source=Image1) or (Source is TCDBImage) or
(Source is TFileListBox) or (Source is TListBox);
end;
procedure TForm1.archivefileDragOver(Sender, Source: TObject; X,
Y: Integer; State: TDragState; var Accept: Boolean);
begin
accept := True; { but... }
if ((Source is TGroupBox) and not (Sender is TGroupBox)) or
(((Sender is TEdit)or (Sender is TGroupBox)) and (Source is TListBox)) or { not from our OWN list }
(Source=Trashcan) then
accept := False;
end;
{ Used to create 'work' filenames for saving images and memos
from the database into our archive or to disk... }
function TForm1.GetDummyFilename(generatefrom: string; ext: string): string;
var spos:Integer;
begin
if (generatefrom='Image') or (generateFrom='') then
generatefrom:='image'
else
begin
generatefrom := copy(generatefrom,1,8); { max 8 }
spos:=pos(' ',generateFrom);
while spos >0 do { eliminate spaces }
begin
delete(generatefrom,spos,1);
spos:=pos(' ',generateFrom);
end;
end;
result := AnsiLowerCase(Getdir+generatefrom+'.'+ext);
end;
function Confirmfilename(filename: String; archiving: Boolean): Boolean;
var dlg: Integer;
begin
Result := True; { default for archiving }
if (not Archiving) and
(MessageDlg('Save to '+filename+'?', mtConfirmation,[mbYes, mbNo], 0)<>id_Yes) then
Result := False;
end;
{ The handler for dropping things on the file list or archive list }
procedure TForm1.handleDropField(Source: TObject; archivetoo: Boolean);
var filename: String;
begin
filename := ''; { in case it is NOT one of those below... }
if Source is TCDBMemo then
begin
filename := GetDummyFilename(Fishname.Text,'TXT');
if not confirmFilename(filename,archivetoo) then exit;
CDBMemo1.Lines.SaveToFile(filename);
end else if Source is TCDBImage then
begin
filename := GetDummyFilename(Fishname.Text,'BMP');
if not confirmFilename(filename,Archivetoo) then exit;
CDBImage1.Picture.Bitmap.SaveToFile(filename);
end else if Source = Image1 then
begin
filename := GetDummyFilename('Image','BMP');
if not confirmFilename(filename,Archivetoo) then exit;
Image1.Picture.Bitmap.SaveToFile(filename);
end;
if (filename<>'') and (ArchiveToo) then
CompressOneFile(filename);
end;
procedure TForm1.archivefileDragDrop(Sender, Source: TObject; X,
Y: Integer);
begin
if Source is TFileListBox then
CompressFiles
else
HandleDropField(Source, True); { save to temp file AND archive... }
end;
procedure TForm1.DLDragDrop(Sender, Source: TObject; X, Y: Integer);
var dlg: Integer;
begin
if Source=Sender then exit; { seems reasonable, and IS necessary }
if Source is TListBox then
ExpandDelete(cmExpand,False) { selected archive files }
else if Source=ArchiveGroup then
ExpandDelete(cmExpand,True) { all archived files }
else
HandleDropField(Source, False); { save field to a file }
FL.Update; { get up to date... }
end;
procedure TForm1.TrashcanDragDrop(Sender, Source: TObject; X, Y: Integer);
var count: Integer;
tempBitmap: TBitMap; { just to get an empty one }
begin
if Source is TListBox then
ExpandDelete(cmDelete,False)
else if Source=ArchiveGroup then
ExpandDelete(cmDelete,True) { all files }
{ and strictly speaking, should now delete the archive if it is
empty, but I'll leave that as an exercise... }
else if Source is TFileListBox then { delete some or all... }
begin
for count:=0 to FL.Items.count-1 do
if FL.selected[count] and
(MessageDlg('Delete '+GetDir+FL.Items[count],mtConfirmation,[mbYes,mbNo],0)=id_Yes) then
DeleteFile(GetDir+FL.Items[count]);
FL.Update;
end
else if (Source is TCDBMemo) and
(MessageDlg('Cut to clipboard?',mtConfirmation,[mbYes,mbNo],0)=id_Yes) then
begin
CDBMemo1.SelectAll;
CDBMemo1.cutToClipboard { safer than .clear, for demo purposes }
end
else if (Source is TCDBImage) and
(MessageDlg('Cut to clipboard?',mtConfirmation,[mbYes,mbNo],0)=id_Yes) then
CDBImage1.cutToClipboard { not quite a delete, but just for example... }
else if Source=Image1 then
begin
tempBitMap := TBitMap.Create;
try
Image1.Picture.Bitmap.Assign(tempBitMap);
Memo1.visible := True
finally
tempBitMap.free;
end;
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
var count: Integer;
begin
if FileList<> nil then
for count:= 0 to FileList.count-1 do
Filelist.objects[count].free; { get rid of these (if any)... }
FileList.free; { and the list itself }
end;
procedure TForm1.ListBox1Click(Sender: TObject);
var cfinfo: TCompressedFileInfo;
begin
if listBox1.ItemIndex >=0 then
begin
CMethod.Color := ShowFileInfoColor; { make it clear we are showing off a bit... }
Percentage.Color := ShowFileInfoColor;
Time.Color := ShowFileInfoColor;
TimeLabel.Caption := 'Full Size:';
cfinfo:=TCompressedFileinfo(FileList.objects[listBox1.ItemIndex]); { how to get at the other stuff... }
if cfinfo.Fullsize>0 then
Percentage.caption:=IntToStr(100-100*cfinfo.CompressedSize div cfinfo.Fullsize)+'%'
else
Percentage.caption:='(empty)';
Time.caption:= IntToStr((512+cfinfo.Fullsize) div 1024)+' Kb';
if saveCompressionMethod <0 then
savecompressionMethod :=cMethod.ItemIndex;
cMethod.ItemIndex :=Integer(cfinfo.CompressedMode);
end;
end;
procedure TForm1.ResetFileInfo;
begin
if saveCompressionMethod <0 then exit;
cMethod.ItemIndex:=savecompressionMethod;
saveCompressionMethod := -1;
CMethod.Color := clBtnFace;
Percentage.Color := clWindow;
Time.Color := clWindow;
TimeLabel.Caption := 'Time:';
showInfo; { get the right stuff too... }
Time.Caption:=''; { but this is meaningless at this point... }
end;
{ Refreshing a CDBImage so it will be compressed (assuming previously uncompressed) }
procedure TForm1.CDBImage1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Button=mbRight then { ok, refresh our field }
begin
CDBImage1.CopyToClipBoard;
CDBImage1.PasteFromClipBoard;
Table1.post;
end;
end;
procedure TForm1.CDBMemo1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Button=mbRight then { ok, refresh our field }
begin
CDBMemo1.Lines[0]:=CDBMemo1.Lines[0]; { setting .Modified doesn't do it... }
Table1.post;
end;
end;
procedure TForm1.Table1AfterPost(DataSet: TDataset);
begin
Showinfo;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage('Drag and Drop at will: compression and expansion'+#13+
'is automatic.'+#13+#13+
'Uses TCompress, TCDBMemo and TCDBImage.'+#13+#13+
'Component Registration and License: $NZ70 (appr. $US45)'+#13+
'South Pacific Information Services Ltd'+#13+
'Fax: +64-3-384-5138 Email: nzsm@spis.southern.co.nz');
end;
procedure TForm1.FLClick(Sender: TObject);
begin
ResetFileInfo;
end;
procedure TForm1.Compress1CheckFile(var filepath: OpenString;
mode: TCProcessMode);
var modestr: String;
dlg: Integer;
begin
case mode of
cmExpand: begin
modestr := 'Expand';
filepath:=Getdir+extractfilename(filepath); { go where we should }
end;
cmCompress: begin
modestr := 'Compress';
filepath:={Getdir+}extractfilename(filepath); { use GetDir if you want full path... }
end;
cmDelete: modestr := 'Delete';
end;
showInfo;
Screen.cursor := crDefault; { in case this is second call in a sequence }
dlg := MessageDlg(modestr+' '+filepath+'?', mtConfirmation,[mbYes, mbNo, mbCancel], 0);
case dlg of
id_No: filepath :=CompressSkipFlag; { flag 'not this one'}
id_Cancel: filepath :=CompressNoMoreFlag; { flag 'no more!' }
id_Yes: Screen.Cursor := crHourGlass; { for operation itself }
end;
end;
procedure TForm1.Panel1Click(Sender: TObject);
begin
ResetFileInfo;
end;
procedure TForm1.FormClick(Sender: TObject);
begin
ResetFileInfo;
end;
procedure TForm1.GroupBox1Click(Sender: TObject);
begin
ResetFileInfo;
end;
procedure TForm1.TrashcanDragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
accept := True;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Application.HelpFile:='COMPRESS.HLP';
Application.HelpJump('1050');
end;
end.